<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%@page import="weaver.conn.*"%>
<%@page import="weaver.jobs.common.ModeUtil"%>
<%@page import="net.sf.json.JSONObject"%>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="weaver.hrm.resource.ResourceComInfo" %>
<%@ page import="weaver.docs.category.SecCategoryComInfo" %>
<%@ page import="weaver.docs.docs.*" %>
<%@ page import="weaver.general.TimeUtil" %>
<%@ page import="java.net.URL" %>
<%@ page import="java.net.HttpURLConnection" %>
<%@ page import="java.io.*" %>
<%@ page import="weaver.jobs.common.DocUtil" %>
<%@page import="weaver.hrm.User" %>
<%@page import="weaver.hrm.HrmUserVarify" %>

<%
    BaseBean bb = new BaseBean();
    User user = HrmUserVarify.getUser( request, response );
	ModeUtil modeUtil = new ModeUtil();
    RecordSet rs = new RecordSet();

    int formmodeid = 1;
    String tablename = "";
    String modedatacreatertype = "1";
    String seccategory = "1508";
    String source = "oldoa";
    String subcompanyid1 = "1";
    String downloadpath = "http://192.168.15.80/netoffice//Pages/AttachStorageInfo/ReadAttachFile.aspx?AttachGuid=";
    
    String modesql = "select a.*,b.tablename from modeinfo a,workflow_bill b where a.formid=b.id and a.id="+formmodeid;
    rs.executeSql(modesql);
    while(rs.next()){
    	String id = Util.null2String(rs.getString("seccategory"));
    	tablename = Util.null2String(rs.getString("tablename"));
    	if(!id.equals("") && !id.equals("0")){
    		seccategory = id;
    	}
    }	
    
    Map<String, String> userMap = new HashMap();
    Map<String, String> deptMap = new HashMap();
    Map<String, String> compMap = new HashMap();
    
    String sql = "select * from hrmresource where subcompanyid1="+subcompanyid1;
    rs.executeSql(sql);
    while(rs.next()){
    	String id = Util.null2String(rs.getString("id"));
    	String lastname = Util.null2String(rs.getString("lastname"));
    	userMap.put(lastname, id);
    }

    sql = "select * from hrmdepartment where subcompanyid1="+subcompanyid1;
    rs.executeSql(sql);
    while(rs.next()){
    	String id = Util.null2String(rs.getString("id"));
    	String departmentname = Util.null2String(rs.getString("departmentname"));
    	deptMap.put(departmentname, id);
    }

    sql = "select * from hrmsubcompany";
    rs.executeSql(sql);
    while(rs.next()){
    	String id = Util.null2String(rs.getString("id"));
    	String subcompanyname = Util.null2String(rs.getString("subcompanyname"));
    	compMap.put(subcompanyname, id);
    }

    List<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();
    Map<String,Object> dataMap = null;
    Map<String,String> fileMap = new HashMap<String,String>();
    Map<String,String> mainidMap = new HashMap<String,String>();

    //String wheresql = " and OperateDate>'2019-02-31' ";
    //String wheresql = " and rowguid='f1f25852-a657-4c77-8650-92adad3c0fee' ";
    String wheresql = "";
    
    RecordSetDataSource rsd = new RecordSetDataSource(source);
    String sqlsw = " " +
	    " select " +
	    "   t1.TxtTitle,\n" + //-- 来文标题
	    "   t1.TxtLaiWenDept,\n" + //-- 来文单位
	    "   t1.TxtLaiWenHao,\n" + //-- 来文文号 锡政传阅〔2020〕1083号
	    //"   t1.DispatchSequenceNumber,"+ //1083
	    //"   t1.DispatchDepartmentEraWord,"+ //锡政传阅
	   // "   t1.DispatchDepartmentName,\n" + //-- 派遣部门
	    "   t1.JpdShouWenYear,\n" + //-- 年份
	    "   t1.TxtShouWenDate,\n" + //-- 来文日期
	    "   t1.JpdBaoCunQX,\n" + //-- 保存期限
	    //"   t1.filetype,\n" + //-- 文件种类
	    //"   t1.feedbacktype, \n" + //-- 反馈类型
	    "   t1.JpdShouWenXl,\n" + //-- 收文序列  上级来文
	    "   t1.TxtShouWenNumber,\n" + //-- 收文序号  1140
	    "   t1.ShouWenFileNumber, \n" +//-- 收文文件号 上级来文〔2020〕1140号
	    "   t1.InitUserDisplayName, \n" + //-- 拟稿人
	    //"   t1.InduceState, \n" + //状态
	    "   t1.rowguid,"+ //
	    //"   t1.submitremark, \n" + //-- 登记意见
	    //"	t1.submitstatus,"+
	    //"	t1.Arch_Type," +
         //   "t1.Arch_Kind," +
         //   "t1.jinji," +
           // "t1.inboxguid," +
            "t1.processversioninstanceguid," +
            "t1.jpdarchivetype, "+//文种、文类、紧急程度、归档类目
	    "   t2.ouname " +
          //  "t1.huifuflag \n" + //收文单位
	    " from wd_25 t1,frame_ou t2 \n" +
	    " where t1.SubWebFlowOuGuid = t2.ouguid\n" +
	    wheresql+
	   // " and t1.rowguid ='5401e3bb-902d-4bb4-9e14-48531287aab9'" +
	    " order by TxtShouWenDate desc ";
    rsd.execute(sqlsw);
    int n=0;
    while(rsd.next())
    {
        String TxtTitle = Util.null2String(rsd.getString("TxtTitle"));
        String TxtLaiWenDept = Util.null2String(rsd.getString("TxtLaiWenDept"));
        String TxtLaiWenHao = Util.null2String(rsd.getString("TxtLaiWenHao"));
        //String DispatchSequenceNumber = Util.null2String(rsd.getString("DispatchSequenceNumber"));
       // String DispatchDepartmentEraWord = Util.null2String(rsd.getString("DispatchDepartmentEraWord"));
       // String DispatchDepartmentName = Util.null2String(rsd.getString("DispatchDepartmentName"));
        String JpdShouWenYear = Util.null2String(rsd.getString("JpdShouWenYear"));
        String TxtShouWenDate = Util.null2String(rsd.getString("TxtShouWenDate"));
        String JpdBaoCunQX = Util.null2String(rsd.getString("JpdBaoCunQX"));
        //String filetype = Util.null2String(rsd.getString("filetype"));
        //String feedbacktype = Util.null2String(rsd.getString("feedbacktype"));
        String JpdShouWenXl = Util.null2String(rsd.getString("JpdShouWenXl"));
        String TxtShouWenNumber = Util.null2String(rsd.getString("TxtShouWenNumber"));
        String ShouWenFileNumber = Util.null2String(rsd.getString("ShouWenFileNumber"));
        String InitUserDisplayName = Util.null2String(rsd.getString("InitUserDisplayName"));
        //String InduceState = Util.null2String(rsd.getString("InduceState"));
        String rowguid = Util.null2String(rsd.getString("rowguid"));
        //String submitremark = Util.null2String(rsd.getString("submitremark")); //
       // String ouname = Util.null2String(rsd.getString("ouname"));  //收文单位
       // String arch_type = Util.null2String(rsd.getString("arch_type"));  //
       // String arch_kind = Util.null2String(rsd.getString("arch_kind"));  //
       // String jinji = Util.null2String(rsd.getString("jinji"));  //
       // String inboxguid = Util.null2String(rsd.getString("inboxguid"));  //
        String processversioninstanceguid = Util.null2String(rsd.getString("processversioninstanceguid"));  //
        //String huifuflag = Util.null2String(rsd.getString("huifuflag"));  //重要反馈跟踪数据
        String submitstatus = Util.null2String(rsd.getString("submitstatus"));  //收文单位
        String jpdarchivetype = Util.null2String(rsd.getString("jpdarchivetype"));//归档类目

        dataMap = new HashMap<String,Object>();
        dataMap.put("txttitle", TxtTitle);
        dataMap.put("txtlaiwendept", TxtLaiWenDept);
        dataMap.put("txtlaiwenhao", TxtLaiWenHao);
      //  dataMap.put("dispatchsequencenumber", DispatchSequenceNumber);
       // dataMap.put("dispatchdepartmenteraword", DispatchDepartmentEraWord);
       // dataMap.put("dispatchdepartmentname", DispatchDepartmentName);
        dataMap.put("jpdshouwenyear", JpdShouWenYear);
        dataMap.put("txtshouwendate", TxtShouWenDate);
        dataMap.put("jpdbaocunqx", JpdBaoCunQX);
      //  dataMap.put("filetype", filetype);
      //  dataMap.put("feedbacktype", feedbacktype);
        dataMap.put("jpdshouwenxl", JpdShouWenXl);
        dataMap.put("txtshouwennumber", TxtShouWenNumber);
        dataMap.put("shouwenfilenumber", ShouWenFileNumber);
        dataMap.put("inituserdisplayname", InitUserDisplayName);
      //  dataMap.put("inducestate", InduceState);
        dataMap.put("rowguid", rowguid);
     //   dataMap.put("ouname", ouname);
     //   dataMap.put("submitremark", submitremark);
      //  dataMap.put("huifuflag", huifuflag);
        dataMap.put("submitstatus", submitstatus);
       // dataMap.put("arch_type", arch_type);
      //  dataMap.put("arch_kind", arch_kind);
       // dataMap.put("jinji", jinji);
      //  dataMap.put("inboxguid", inboxguid);
        dataMap.put("gdlm", jpdarchivetype);

        dataList.add(dataMap);
        fileMap.put(rowguid, processversioninstanceguid);

        if(n>20){
            //break;
        }
        n++;
    }
    
    Map<String, List<Map>> zws = new HashMap();
    Map<String, List<Map>> fjs = new HashMap();

    sql = "select attachguid,ATTACHFILENAME,FILEPATH,CLIENGTAG,CLIENGGUID from frame_attachinfo "+
    	"where CLIENGGUID in (select ProcessVersionInstanceGuid from wd_25 where 1=1 "+wheresql+") order by uploaddatetime";
    rsd.execute(sql);
    while(rsd.next()){
    	String attachguid = Util.null2String(rsd.getString("attachguid"));
    	String attachfilename = Util.null2String(rsd.getString("attachfilename"));
    	String cliengtag = Util.null2String(rsd.getString("cliengtag"));
    	String cliengguid = Util.null2String(rsd.getString("cliengguid"));
    	
    	Map temp = new HashMap();
    	temp.put("attachguid", attachguid);
    	temp.put("attachfilename", attachfilename);
    	temp.put("cliengtag", cliengtag);
    	
    	if(cliengtag.equals("zhangwai")){
    		continue;
    	}else if(cliengtag.equals("formal")||cliengtag.equals("origin")||cliengtag.equals("revision")||cliengtag.equals("format")){
    		List list = new ArrayList();
    		if(zws.containsKey(cliengguid)){
    			list = zws.get(cliengguid);
    		}
    		list.add(temp);
    		zws.put(cliengguid, list);
    	}else{
    		List list = new ArrayList();
    		if(fjs.containsKey(cliengguid)){
    			list = fjs.get(cliengguid);
    		}
    		list.add(temp);
    		fjs.put(cliengguid, list);
    	}
    }

    List<Map<String,String>> mainList = new ArrayList<Map<String,String>>();
    Map<String,String> mainMap = null;
    for(int i=0;i<dataList.size();i++)
    {
        Map<String,Object> dmap = dataList.get(i);
        String rowguid = Util.null2String(dmap.get("rowguid"));

        String txttitle =  Util.null2String(dmap.get("txttitle"));
        String txtshouwendate =  Util.null2String(dmap.get("txtshouwendate"));
        String filetype =  Util.null2String(dmap.get("filetype"));
        String feedbacktype =  Util.null2String(dmap.get("feedbacktype"));
        String inituserdisplayname =  Util.null2String(dmap.get("inituserdisplayname"));
        String jpdshouwenxl =  Util.null2String(dmap.get("jpdshouwenxl"));
        String txtlaiwendept =  Util.null2String(dmap.get("txtlaiwendept"));
        String ouname =  Util.null2String(dmap.get("ouname"));
        String inducestate =  Util.null2String(dmap.get("inducestate"));

        if(!"".equals(txtshouwendate))
        {
            txtshouwendate = txtshouwendate.substring(0,10);
        }
        dmap.put("txtshouwendate",txtshouwendate);
        String ngrid = Util.null2String(userMap.get(inituserdisplayname)) ;
        if("".equals(ngrid)){
            ngrid = "1";
        }
        dmap.put("ngrid",ngrid);
        
        String lwdw = Util.null2String(compMap.get(txtlaiwendept));
        if(!"".equals(lwdw)){
	        dmap.put("lwdw",lwdw);
        }
        String swdw = Util.null2String(deptMap.get(ouname)) ;
        if(!"".equals(swdw)){
	        dmap.put("swdw",swdw);
        }
        String dqzt = "" ;
        if(inducestate.equals("20")){
        	dqzt = "拟办";
        }else if(inducestate.equals("90") || inducestate.equals("阅办")){
        	dqzt = "阅办";
        }else if(inducestate.equals("70")){
        	dqzt = "已办结";
        }else if(inducestate.equals("10")){
        	dqzt = "登记";
        }else if(inducestate.equals("40")){
        	dqzt = "分送";
        }else if(inducestate.equals("80")){
        	dqzt = "归档";
        }
        
        String processversioninstanceguid = Util.null2String(fileMap.get(rowguid));
		if(!processversioninstanceguid.equals("")){
			String zw = "";
			String fj = "";
			if(zws.containsKey(processversioninstanceguid)){
				List<Map> zwlist = zws.get(processversioninstanceguid);
			    for(Map temp : zwlist){
			    	String attachguid = Util.null2String(temp.get("attachguid"));
					String attachfilename = Util.null2String(temp.get("attachfilename"));
					String cliengtag = Util.null2String(temp.get("cliengtag"));
					
					String downloadUrl = downloadpath+attachguid;
					//out.print("<br>下载地址----->"+downloadUrl);
					
					Map<String, Object> retMap = DocUtil.downloadByUrl(downloadUrl);
					if(!Util.null2String(retMap.get("code")).equals("0")){
						//out.print("<br>"+attachfilename+"----->"+retMap.get("msg"));
						bb.writeLog("正文："+attachfilename+"----->"+retMap.get("msg"));
					}else{
						String filepath = Util.null2String(retMap.get("data"));
						int imagefileid = DocUtil.saveImageFile(attachfilename, filepath);
	        			
						int docid = DocUtil.createDocByImagefileId(imagefileid, user, "1", "0", attachfilename, "", Util.getIntValue("1618",-1));
						zw += (zw.equals("")?docid:(","+docid));
					}
			    }
			}
			if(fjs.containsKey(processversioninstanceguid)){
				List<Map> fjlist = fjs.get(processversioninstanceguid);
			    for(Map temp : fjlist){
			    	String attachguid = Util.null2String(temp.get("attachguid"));
					String attachfilename = Util.null2String(temp.get("attachfilename"));
					String cliengtag = Util.null2String(temp.get("cliengtag"));
					
					String downloadUrl = downloadpath+attachguid;
					//out.print("<br>下载地址----->"+downloadUrl);
					
					Map<String, Object> retMap = DocUtil.downloadByUrl(downloadUrl);
					if(!Util.null2String(retMap.get("code")).equals("0")){
						//out.print("<br>"+attachfilename+"----->"+retMap.get("msg"));
						bb.writeLog("正文："+attachfilename+"----->"+retMap.get("msg"));
					}else{
						String filepath = Util.null2String(retMap.get("data"));
						int imagefileid = DocUtil.saveImageFile(attachfilename, filepath);
	        			
						int docid = DocUtil.createDocByImagefileId(imagefileid, user, "1", "0", attachfilename, "", Util.getIntValue("1618",-1));
						fj += (fj.equals("")?docid:(","+docid));
					}
			    }
			}
			if(!zw.equals("")){
				dmap.put("zw",zw);
			}
			if(!fj.equals("")){
				dmap.put("fj",fj);
			}
		}

        if("null".equals(filetype) || filetype==null){
            filetype = "";
        }
        dmap.put("filetype",filetype);

        if("".equals(feedbacktype) || feedbacktype==null){
            feedbacktype = "";
        }
        dmap.put("feedbacktype",feedbacktype);

        if("null".equals(jpdshouwenxl) || jpdshouwenxl==null){
            jpdshouwenxl = "";
        }
        dmap.put("jpdshouwenxl",jpdshouwenxl);

        Map<String, Object> retMap = modeUtil.addModeInfo(dmap, tablename, formmodeid, user.getUID(), false, true, "rowguid");
        if(!Util.null2String(retMap.get("code")).equals("0")){
        	//out.print(i+"--->【"+txttitle+"】:失败，失败原因："+Util.null2String(retMap.get("msg"))+"<br/>");
        }else{
           // out.print(i+"--->【"+txttitle+"】:成功，id："+Util.null2String(retMap.get("data"))+"<br/>");
        	
        	mainidMap.put(rowguid, Util.null2String(retMap.get("data")));
        }
    }


    Map<String, List<Map>> logs = new HashMap();
    sql = " select  workitemguid,activityname,sendername,transactorname,operationtype," +
    	"     createdate,enddate,opinion,workitemtype,processVersionInstanceGuid " +
    	"  from workflow_workitem_history " +
    	" where processVersionInstanceGuid in (select processVersionInstanceGuid from wd_25 where 1=1 "+wheresql+") ";
    rsd.execute(sql);
    while(rsd.next())
    {
    	String processVersionInstanceGuid = Util.null2String(rsd.getString("processVersionInstanceGuid"));
        String uuid = Util.null2String(rsd.getString("workitemguid"));
        String bz = Util.null2String(rsd.getString("activityname"));
        String sybtjr = Util.null2String(rsd.getString("sendername"));
        String clry = Util.null2String(rsd.getString("transactorname"));
        String czlx = Util.null2String(rsd.getString("operationtype"));
        String createdate = Util.null2String(rsd.getString("createdate"));
        String enddate = Util.null2String(rsd.getString("enddate"));
        String clyj = Util.null2String(rsd.getString("opinion"));
        
        Map temp = new HashMap();
        temp.put("workitemguid", uuid);
        temp.put("activityname", bz);
        temp.put("sendername", sybtjr);
        temp.put("transactorname", clry);
        temp.put("operationtype", czlx);
        temp.put("createdate", createdate);
        temp.put("enddate", enddate);
        temp.put("opinion", clyj);
        
        List<Map> list = new ArrayList();
        if(logs.containsKey(processVersionInstanceGuid)){
        	list = logs.get(processVersionInstanceGuid);
        }
        list.add(temp);
        logs.put(processVersionInstanceGuid, list);
    }

    sql = " select workitemguid,activityname,sendername,transactorname,operationtype," +
    "     createdate,enddate,opinion,workitemtype,processVersionInstanceGuid " +
    "  from workflow_workitem " +
    " where processVersionInstanceGuid in (select processVersionInstanceGuid from wd_25 where 1=1 "+wheresql+") ";
    rsd.execute(sql);
    while(rsd.next())
    {
    	String processVersionInstanceGuid = Util.null2String(rsd.getString("processVersionInstanceGuid"));
    	String uuid = Util.null2String(rsd.getString("workitemguid"));
    	String bz = Util.null2String(rsd.getString("activityname"));
    	String sybtjr = Util.null2String(rsd.getString("sendername"));
    	String clry = Util.null2String(rsd.getString("transactorname"));
    	String czlx = Util.null2String(rsd.getString("operationtype"));
    	String createdate = Util.null2String(rsd.getString("createdate"));
    	String enddate = Util.null2String(rsd.getString("enddate"));
    	String clyj = Util.null2String(rsd.getString("opinion"));
    	
    	Map temp = new HashMap();
    	temp.put("workitemguid", uuid);
    	temp.put("activityname", bz);
    	temp.put("sendername", sybtjr);
    	temp.put("transactorname", clry);
    	temp.put("operationtype", czlx);
    	temp.put("createdate", createdate);
    	temp.put("enddate", enddate);
    	temp.put("opinion", clyj);
    	
    	List<Map> list = new ArrayList();
    	if(logs.containsKey(processVersionInstanceGuid)){
    		list = logs.get(processVersionInstanceGuid);
    	}
    	list.add(temp);
    	logs.put(processVersionInstanceGuid, list);
    }
    for(int i=0;i<dataList.size();i++){
    	Map<String,Object> dmap = dataList.get(i);
        String rowguid = Util.null2String(dmap.get("rowguid"));
        String mainid = Util.null2String(mainidMap.get(rowguid));
        String processVersionInstanceGuid = Util.null2String( fileMap.get(rowguid) );
        sql = "delete from "+tablename+"_dt1 where mainid='"+mainid+"'";
        rs.executeSql(sql);
        if(!logs.containsKey(processVersionInstanceGuid)){
        	continue;
        }
        List<Map> list = logs.get(processVersionInstanceGuid);
        
        for(Map temp : list)
        {
            String uuid = Util.null2String(temp.get("workitemguid"));
            String bz = Util.null2String(temp.get("activityname"));
            String sybtjr = Util.null2String(temp.get("sendername"));
            String clry = Util.null2String(temp.get("transactorname"));
            String sybtjryid = "" ;
            String clryid = "" ;

            if(!"".equals(sybtjr))
            {
            	sybtjryid = Util.null2String(userMap.get(sybtjr));
            }
            if(!"".equals(clry))
            {
            	clryid = Util.null2String(userMap.get(clry));
            }

            String czlx = Util.null2String(temp.get("operationtype"));
            String createdate = Util.null2String(temp.get("createdate"));

            String sdsj ="" ;
            String sdrq = "" ;
            if(!"".equals(createdate))
            {
                sdrq = createdate.substring(0,10);
                sdsj = createdate.substring(11,16);
            }
            String clsj ="" ;
            String clrq = "" ;
            String enddate = Util.null2String(temp.get("enddate"));
            if(!"".equals(enddate))
            {
                clrq = enddate.substring(0,10);
                clsj = enddate.substring(11,16);
            }

            String clyj = Util.null2String(temp.get("opinion"));
            Map<String,Object> detailMap = new HashMap<String,Object>();
            detailMap.put("uuid",uuid);
            detailMap.put("mainid",mainid);
            detailMap.put("bz",bz);
            detailMap.put("sybtjry",sybtjr);
            detailMap.put("clry",clry);

            detailMap.put("sybtjryid",sybtjryid);
            detailMap.put("clryid",clryid);
            detailMap.put("czlx",czlx);
            detailMap.put("sdsj",sdsj);
            detailMap.put("sdrq",sdrq);
            detailMap.put("clsj",clsj);
            detailMap.put("clrq",clrq);
            detailMap.put("clyj",clyj);
            
            modeUtil.insertsql(detailMap, tablename+"_dt1");
        }
    }
    

%>

<%!
public void writeLog(Object obj) {
	if(true){
		new BaseBean().writeLog(this.getClass().getName(), obj);
	}
}
%>



